%
%  Latex Primer
%
%  Created by Vincenzo Carrino on 2009-08-06.
%
\documentclass[]{article}

% Use utf-8 encoding for foreign characters
\usepackage[utf8]{inputenc}

% Setup for fullpage use
\usepackage{fullpage}

% Uncomment some of the following if you use the features
%
% Running Headers and footers
%\usepackage{fancyhdr}

% Multipart figures
%\usepackage{subfigure}

% More symbols
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{latexsym}

% Surround parts of graphics with box
\usepackage{boxedminipage}

% Package for including code in the document
\usepackage{listings}
\lstset{language=[Latex]Tex, frame=lines, captionpos=b}

% Footnotes appear at the end of the document
\usepackage{endnotes}

% Wrap URL
\usepackage{url}

% If you want to generate a toc for each chapter (use with book)
\usepackage{minitoc}

% This is now the recommended way for checking for PDFLaTeX:
\usepackage{ifpdf}

\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi


\title{A \LaTeX\  Primer}
\author{ Vincenzo Carrino }

\date{10 August 2009}
 
\begin{document}

\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .tif}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi

\maketitle

\begin{figure}[h] 
%   \rule{\textwidth}{0.005in}
    \centering
    \includegraphics[width=4in]{ctan_lion} 
    \caption{CTAN lion drawing by Duane Bibby; courtesy of www.ctan.org}
    \label{fig:ctanlion}
%   \rule{\textwidth}{0.005in}
\end{figure}
\newpage

\begin{abstract}
Presented is a variety of topics covered in moderate detail, hoping to expose enough to get stared while not being verbose.  Discussed are a variety of topics: its inception, how to get started in LaTeX , and how to become well versed on the subject.  Plus a good deal more.  Hopefully with this you will begin to see the beauty and versitlity of LaTeX. 
\end{abstract}
The latest version can be found at \url{http://latexref.googlecode.com/svn/trunk/LatexPrimer/primer.pdf}
\newpage

\tableofcontents
\newpage

\section{\LaTeX\ and its Predecessors}
\subsection{Early typesetting}
Typesetting, both mechanical and digital, have long histories.  Mechanical typesetting is the subject for another time, though it directly leads into processes such as Phototypesetting.  With the outbreak of the digital technology several machines emerged to take the place of those from Letterpress era. Though often these machines were both bulky and costly.  This made way for specialized typesetting `programs' often running on the then modern Unix machines.  Joseph Ossanna developed Troff in the mid--1970s to drive a typesetter at Bell Laboratories.  In various derivatives it still exists today.
\subsection{TeX}
Donald E. Knuth was already an prominent name in Computer Science with his publication of {\em The Art of Computer Programming}.  He developed the TeX system at the end of the 70s, it rapidly set high standards for typesetting programs.  It specifically excelled at typesetting mathematics.  But TeX was deemed difficult to learn, and puts appearance ahead of structure.
\subsection{LaTeX}
In the early 1980s  while at SRI International Leslie Lamport developed the LaTeX macro package for TeX.  It offered a simpler manner of doing things, and corrected many of the flaws present in TeX.  Often pronunciation is a concern while discussing LaTex, Knuth promotes a /t$\varepsilon$x/ pronunciation similar to technique, while Lamport stated he does not strictly discourage any one pronunciation.  LaTeX quickly became the dominant method for using TeX.  It to was rather strong at handling complex mathematical input, though often aided by the amsmath library.
\begin{equation*}
    e^{ix} = \cos x + i \sin x
\end{equation*}
\begin{equation*}
    f(x) = \sum_{n=0}^{\infty} \frac{f^n (a)}{n!} (x-a)^n
\end{equation*}
The LaTeX for the single variable Taylor expansion is as followed:
\newline
\begin{lstlisting}[caption={Taylor series equation}, label=lst:taylor]
    \begin{equation*}
        f(x) = \sum_{n=0}^{\infty} \frac{f^n(a)}{n!} (x-a)^n
    \end{equation*}  
\end{lstlisting}
It results in output that is clean, and formated beautifully.  

\section{Getting Started}
If LaTeX is already on your system, this section will mostly likely be redundant.
\subsection{Determining your installation}
In some form LaTeX is available on nearly every system.  \url{http://www.latex-project.org/ftp.html} gives an overview of several distributions of TeX.  In a brief overview: on MacOS X there is a full TeX system bundled with LaTeX that is called MacTeX.  On Linux it is commonly provided in your system distribution.  On Windows machines there is proTeXt that provides a TeX system also with LaTeX bundled.  
\subsection{How to write LaTeX}
LaTeX is produced through code, much like Listing~\ref{lst:taylor}. Many of the distributions provide a text editor in some sense.  Though for someone who writes in LaTeX frequently, it is often beneficial to learn an editor that is more developer friendly.  Of course Vim and Emacs are two very viable solutions.  They both provide syntax highlighting, code formating, and many features that increase overall productivity.  One editor that particularly stands out is Textmate exclusively on MacOS X. Its combination of powerful bundles and immense extendability makes it a wonderful editor for nearly every language.  
\subsection{Uses of LaTeX}
LaTeX succeeds because it is very good at what it does.  Its use is very common in academics, as well as general publishing.  It is also highly extendable resulting in very exciting new uses.  Beamer is a LaTeX package that creates wonderful presentation slides, while maintaing the original LaTeX principles.  Skak is a package that allows the creation of chess boards, as well as the ability to typeset entire games.  There are many more, look at Section~\ref{sec:learningmore} to fine where.
\subsection{Basic document structure}
LaTeX requires very little to be considered a complete file.
\begin{lstlisting}[caption={A minimal LaTeX file}, label=lst:minimal]
    \documentclass{article}
    \begin{document}
    
        Body
        
    \end{document}
\end{lstlisting}
Listing~\ref{lst:minimal} shows the bare minimum for a LaTeX file, though it does illustrate several basic LaTeX concepts. First LaTeX source files are usually suffixed with .tex, and during processing creates a bundle of helper files.  Line one is called the preamble it appears in all LaTeX documents.  \lstinline|\documentclass[options(s)]{class}| with class being one of the several document class that are available.  Then the \lstinline|\begin{document}| command is issued.  LaTeX disregards all whitespace, handling the formating itself.  Commands that have the \lstinline|\begin{env} ... \end{env}| structure are called environments, with \lstinline|\begin and \end| being the true commands.  LaTeX commands are preceded with a backslash \textbackslash, optional arguments are contained in brackets [ ], and required arguments are housed in braces \{ \}.  There are also declarations \lstinline|{\em text}| where text has \lstinline|\em| or emphasis applied to it.  It works in a similar manner for several other declarations.
\section{\LaTeX\ Syntax}
\subsection{A more substantial example}
A LaTeX file rarely contains only four lines of commands.  It often contains external packages, and uses a variety of commands to construct something of greater importance.  Here is a striped down example from the code to develop this document.
\begin{lstlisting}[caption={A real world LaTeX file}, label=lst:largeexample, numbers=right]
%
%  Latex Primer
%
%  Created by Vincenzo Carrino.
%
\documentclass[]{article}

% Use utf-8 encoding for foreign characters
\usepackage[utf8]{inputenc}

% Setup for fullpage use
\usepackage{fullpage}

% More symbols
\usepackage{amsmath}

% If you want to generate a toc for each chapter (use with book)
\usepackage{minitoc}

\usepackage{graphicx}

\title{A \LaTeX\  Primer}
\author{ Vincenzo Carrino }

\date{\today}
 
\begin{document}

\maketitle

\begin{figure}[h] 
    \centering
    \includegraphics[width=4in]{ctan_lion} 
    \caption{CTAN lion drawing by Duane Bibby; courtesy of www.ctan.org}
    \label{fig:ctanlion}
\end{figure}
\newpage

\begin{abstract}
Presented is a variety of topics covered in moderate detail ...
\end{abstract}
\newpage

\tableofcontents
\newpage

\section{\LaTeX\ and its Predecessors}
\subsection{Early typesetting}
Typesetting, both mechanical and digital, have long histories ...

...

\end{document}
\end{lstlisting} 
\subsection{Syntax Discussion}
\subsubsection{Comments}
Latex has two styles of comments depending on your specific need.  The percent sign \% causes of the remainder of the line to be, overlooked.  It is convenient to make comments from a couple of lines.  Latex also provides block comments in the form of \lstinline|\begin{comment} ... \end{comment}|, entering into the comment environment.  As with all developing it is often good practice to inject the occasional comment into your source file.
\subsubsection{Preamble}
In addition to the standard \lstinline|\documentclass[opt(s)]{class}|, there are a great deal of packages added.  The standard method of including them is \lstinline|\usepackage[opt(s)]{pack(s)}|.  An old developers axiom is do not reinvent to wheel.  If someone already took the time to create a package for it, use it.  Take a few moments to understand it, then move on to your work.  Many packages are imported, then several global constants are set, \lstinline|\title{} \author{} \date{}| are all accessed later in the \lstinline|\maketile| command.
\subsubsection{Document sectioning and table of contents}
Latex has a sectioning hierarchy.
\begin{lstlisting}[caption={Sectioning hierarchy}, label=lst:sectioning, language={}]
    \part{title}
    \chapter{title}
    \section{title}
    \subsection{title}
    \subsubsection{title}
    \paragraph{title}
    \subparagraph{title}
\end{lstlisting}
Like all formating in LaTeX, you do not have to handle to table of contents.  With each new section it handles page numbers as well as indentation for the hierarchy. The \lstinline|\tableofcontents| command prints the table anywhere you place it.
\subsubsection{Figures}
Charts, graphs, images, they all are convenient ways of showing data; and LaTeX makes it easy to insert and reference them.
\begin{lstlisting}[caption={Inserting a figure}, label=lst:figure]
    \begin{figure}[position] % position=h:here,t:top,b:bottom,p:page of floats
        \includegraphics[size]{path/to/file} 
        \caption{caption text}
        \label{fig:identifier}   
    \end{figure}
\end{lstlisting}
Often the graphicx package is used.
\begin{lstlisting}[caption={Using the graphicx package with the recommended way of checking for PDFLaTeX; courtesy of the Textmate contributors}, label=lst:graphicx]
    \usepackage{ifpdf}

    \ifpdf
    \usepackage[pdftex]{graphicx}
    \else
    \usepackage{graphicx}
    \fi
\end{lstlisting}
Often the accepted file types are also specified. A safe bet are Encapsulated PostScript, eps files, if the final document is a PostScript fine.  And pdf, png, or gif for pdf documents, through a pdf--compiler.
\subsubsection{References}
When a figure is set and a \lstinline|\label{}| attribute is added, you can conveniently reference it though the \lstinline|\ref{}| command.  For example:
\begin{lstlisting}[caption={Referencing a figure}, label={lst:ref}]
    \begin{figure}[h]
        \centering
        \includegraphics[width=2in]{holygrail}
        \caption{The most Holy Grail}
        \label{fig:holygrail}
    \end{figure}
    ...
    In Figure \ref{fig:holygrail} you see the object of great desire.
\end{lstlisting}
It is general convention to preface labels with an appropriate tag.
\begin{lstlisting}[caption={Reference tags}, label=lst:tags]
    Figures   \label{fig:label}
    Listings  \label{lst:label}
    Sections  \label{sec:label}
    Tables    \label{tab:label}
\end{lstlisting}
There also exists \lstinline|\pageref{key}| that responds with the page number of where the \lstinline|\label{key}| command is located.  Often there is a citation attributed to a statement and a footnote is needed.  LaTeX makes it very convenient to use them.  Issue the command \lstinline|\footnote{text}|, numbers are automatically inserted, and counted.  The package endnotes, \lstinline|\usepackage{endnotes}|, creates the command \lstinline|\endnote{text}| which instead of collecting notes at the bottom of the page its lists them all with \lstinline|\theendnotes| command.
\subsubsection{Lists}
Built into LaTeX there are three different types of lists.  The itemize list, which is delimited with a symbol.  The enumerate list that counts each item.  And the description list with a key--value feel.  All lists can be nested.
\begin{enumerate}
    \item An item
    \item Another item
    \begin{itemize}
        \item One
        \item Two
        \item Three
    \end{itemize}
    \item Even another item
    \begin{description}
        \item[Alpha] $\alpha$
        \item[Beta] $\beta$
        \item[Gamma] $\gamma$
    \end{description}
    \item Final item
\end{enumerate}
The code to produce the list above:
\begin{lstlisting}[caption={Various lists}, label=lst:lists]
    \begin{enumerate}
        \item An item
        \item Another item
        \begin{itemize}
            \item One
            \item Two
            \item Three
        \end{itemize}
        \item Even another item
        \begin{description}
            \item[Alpha] $\alpha$
            \item[Beta] $\beta$
            \item[Gamma] $\gamma$
        \end{description}
        \item Final item
    \end{enumerate}
\end{lstlisting}
Proper indentation is vital during list construction, as well as in all development.
\subsubsection{Formating}
LaTeX generally handles the formating itself, but there are times that there might be a personal touch that needs to be added.  LaTeX offers many formating commands that change text and those that change the page layout.  Look at Section~\ref{sec:learningmore} for where to find more formatting commands.
\subsection{Math Environments}
LaTeX is particularly strong in academic communities.  Especially the mathematical, scientific, and engineering circles.  And all of them need a unified method of typing and displaying mathematics.  LaTeX has several math environments that can be used.  Often the amsmath, amssymb, and the latexsym packages are mixed in.  
\begin{lstlisting}[caption={Math environments}, label=lst:math]
    \( ... \) or $ ... $   % inline math
    \[ ... \] or $$ ... $$ % displayed math

    %
    % numbered and labeled equation
    %
    \begin{equation}
        \label{eqname}
        ...
    \end{equation}

    %
    % unnumbered equation
    %
    \begin{equation*}
        ...
    \end{equation*}

    %
    % grouped equations for numbering
    %
    \begin{subequations}
        ...
    \end{subequations}
\end{lstlisting}
There are several more that are vastly more specific.  You can of course add dots, vector symbols, primes, to variables.  As well as having a huge array of functions, variables, operators, and symbols to add.  Look at Section~\ref{sec:learningmore} for Math references.  

\section{\LaTeX\ Extended}
\subsection{Letters}
An example of writing a letter in LaTeX, using the brev package.
\begin{lstlisting}[caption={Letter; thanks to Textmate contributors}, label=lstletter]
\documentclass[a4paper,english]{brev}

\usepackage[utf8]{inputenc}
\usepackage{a4}
\pagestyle{empty}

\begin{document}
\address{
    123 Alpha Ave. \\
    12345 Livingston
}
\signature{Vincenzo Carrino}

\begin{letter}{
    Someone \\
    Somewhere \\
    Anytown USA
}
\opening{To whom it may concern,}

\hspace{1cm}{\bf Regarding some pressing issue}

Lorem ipsum dolor sit amet ...
\vspace{4cm}

\closing{Kind Regards}
%\encl{Name of Enclosure}
\end{letter}
\end{document}
\end{lstlisting}
And it looks like:
\begin{figure}[h]
    \centering
    \framebox{\includegraphics[width=4in]{letter}}
    \caption{Example of letter}
    \label{fig:letter}
%   \rule{\textwidth}{0.005in}
\end{figure}
\newpage
\subsection{Beamer}
Beamer is an excellent package that extends LaTeX into a presentation slide creator.  Here is a trimmed example.
\begin{lstlisting}[caption={Beamer example}, label=lst:beamer]

\documentclass[10pt]{beamer}
\usetheme{Warsaw}

\title{Example Presentation}
\subtitle{An Interesting Subtitle}
\author[V. Carrino]{Vincenzo Carrino}
\institute[NJIT]{}

\date{10 August 2009}
\begin{document}
    % Title Page %
    \begin{frame}[plain]
        \titlepage
    \end{frame}
    % Overview %
    \begin{frame}
        \frametitle{Overview of the talk}
        \begin{itemize}
            \item An interesting thing
            \begin{itemize}
                \item Sub item
                \item Another sub item
            \end{itemize}
            \item Another idea
            \smallskip
            %\pause
            \item A final idea
        \end{itemize}
    \end{frame}
    
    ...
    
\end{document}

\end{lstlisting}
A trimmed example, yet here are what the two slides look like:
\begin{figure}[p]
%   \rule{\textwidth}{0.005in}
    \centering
    \framebox{\includegraphics[width=5in]{beamer_title}}
\end{figure}
\begin{figure}[p]
    \centering
    \framebox{\includegraphics[width=5in]{beamer_firstslide}}
    \caption{Examples of beamer slides}
    \label{fig:beamerslides}
%   \rule{\textwidth}{0.005in}
\end{figure}
\newpage

\section{Learning More About \LaTeX}
\label{sec:learningmore}
There are many excellent references out there on LaTeX and all of its extensions.  First \url{ctan.org} must be mentioned.  CTAN is the Comprehensive TeX Archive Network, and as they say ``The Comprehensive TeX Archive Network is the authoritative collection of materials related to the TeX typesetting system. ''  It hosts packages, tutorials, references, and much more.  They host probably an extraordinarily comprehensive symbol sheet produced by Scott Pakin.  It is located at \url{http://www.ctan.org/tex-archive/info/symbols/comprehensive/}.  The University of Colorado produced a math oriented symbol sheet that is very comprehensive in itself.  Located at \url{http://amath.colorado.edu/documentation/LaTeX/Symbols.pdf}.  The current version of LaTeX is LaTeX2e and Winston Chang produced a wonderful general guide to it, located at \url{http://stdout.org/~winston/latex}.  Finally I created two perl scripts that compile references from two wonderful sources.  The first on LaTeX in general, and it has a long history.  Originally created by George Greenwade, and then compiled by Norman Walsh.  The perl script is hosted at \url{http://latexref.googlecode.com/svn/trunk/LatexReference/latexref.pl}.  The second reference on beamer was written by Rouben Rostamin, and the perl compiler script is hosted likewise at, \url{http://latexref.googlecode.com/svn/trunk/LatexReference/beamerref.pl}
\newline
\newline
For convenience: 
\begin{description}
    \item[Everything TeX] \url{http://ctan.org}
    \item[Comprehensive Symbol Sheet] \url{http://www.ctan.org/tex-archive/info/symbols/comprehensive/}
    \item[Mathematics Symbol Sheet]  \url{http://amath.colorado.edu/documentation/LaTeX/Symbols.pdf}
    \item[Valuable Terse Reference]  \url{http://stdout.org/~winston/latex}
    \item[LaTeX Reference] \url{http://latexref.googlecode.com/svn/trunk/LatexReference/latexref.pl}
    \item[Beamer Reference]  \url{http://latexref.googlecode.com/svn/trunk/LatexReference/beamerref.pl}
\end{description}

\bibliographystyle{plain}
\bibliography{}
%\newpage
%\theendnotes
\end{document}

%symbol sheet http://www.ctan.org/tex-archive/info/symbols/comprehensive/
%latex ref
%beamer ref
%latex2e http://stdout.org/~winston/latex
%math symbol sheet http://amath.colorado.edu/documentation/LaTeX

